package com.bw.liveplay

import android.graphics.Color
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.viewpager.widget.ViewPager
import com.blankj.utilcode.util.BarUtils
import com.bw.liveplay.common.mvvm.view.fragment.*
import com.bw.liveplay.common.widegt.BottomButton
import com.flyco.tablayout.SlidingTabLayout

class MainActivity : AppCompatActivity(),BottomButton.BottomSelectListener{
    private var mainFgGroup : LinearLayout ?=null
    private var frameHomeBtn : BottomButton ?=null
    private var frameFindBtn : BottomButton ?=null
    private var frameAddBtn : BottomButton ?=null
    private var frameMsgBtn : BottomButton ?=null
    private var frameMineBtn : BottomButton ?=null
    private var homeFragment : Fragment?=null
    private var findFragment : Fragment?=null
    private var addFragment : Fragment?=null
    private var msgFragment : Fragment?=null
    private var mineFragment : Fragment?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initview()
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
        BarUtils.transparentStatusBar(this)
        //首页
        frameHomeBtn!!.setContent("首页")
            .setNomalColor(Color.BLACK)
            .setSelColor(Color.RED)
            .setNomalImg(R.drawable.icon_tab_home)
            .setSelImg(R.drawable.home1)
            .setNomalSize(9)
            .setSelSize(10)
            .setListener(this)
            .refresh()
        //发现
        frameFindBtn!!.setContent("休闲")
            .setNomalColor(Color.BLACK)
            .setSelColor(Color.RED)
            .setNomalImg(R.drawable.icon_tab_sofa)
            .setSelImg(R.drawable.sofa1)
            .setNomalSize(9)
            .setSelSize(10)
            .setListener(this)
            .refresh()
        //添加
        frameAddBtn!!.setContent("")
            .setNomalColor(Color.BLACK)
            .setSelColor(Color.RED)
            .setNomalImg(R.drawable.add)
            .setSelImg(R.drawable.add)
            .setNomalSize(9)
            .setSelSize(10)
            .setListener(this)
            .refresh()
        //消息
        frameMsgBtn!!.setContent("消息")
            .setNomalColor(Color.BLACK)
            .setSelColor(Color.RED)
            .setNomalImg(R.drawable.icon_tab_movie)
            .setSelImg(R.drawable.movie1)
            .setNomalSize(9)
            .setSelSize(10)
            .setListener(this)
            .refresh()
        //我的
        frameMineBtn!!.setContent("我的")
            .setNomalColor(Color.BLACK)
            .setSelColor(Color.RED)
            .setNomalImg(R.drawable.icon_tab_mine)
            .setSelImg(R.drawable.mine1)
            .setNomalSize(9)
            .setSelSize(10)
            .setListener(this)
            .refresh()

        onSelect(R.id.frame_home_btn)



    }

    fun initview(){

        mainFgGroup =  findViewById(R.id.main_fg_group);
        frameHomeBtn =  findViewById(R.id.frame_home_btn);
        frameFindBtn =  findViewById(R.id.frame_find_btn);
        frameAddBtn =  findViewById(R.id.frame_add_btn);
        frameMsgBtn = findViewById(R.id.frame_msg_btn);
        frameMineBtn = findViewById(R.id.frame_mine_btn);
    }

    //选择按钮
    override fun onSelect(id: Int) {
        clearBottomsel()
        hideFragment()
        val manager = supportFragmentManager
        var ft = manager.beginTransaction()
        when(id){
            R.id.frame_home_btn->{
                frameHomeBtn!!.setSelFlag(true)
                if(homeFragment == null){
                    homeFragment = HomeFeedFragment()
                    ft.add(R.id.main_fg_group,homeFragment as HomeFeedFragment)
                }else{
                    ft.show(homeFragment!!)
                }
            }
            R.id.frame_find_btn->{
                frameFindBtn!!.setSelFlag(true)
                if(findFragment == null){
                    findFragment = FindFragment()
                    ft.add(R.id.main_fg_group,findFragment as FindFragment)
                }else{
                    ft.show(findFragment!!)
                }
            }
            R.id.frame_add_btn->{
                frameAddBtn!!.setSelFlag(true)
                if(addFragment == null){
                    addFragment = AddFragment()
                    ft.add(R.id.main_fg_group,addFragment as AddFragment)
                }else{
                    ft.show(addFragment!!)
                }
            }
            R.id.frame_msg_btn->{
                frameMsgBtn!!.setSelFlag(true)
                if(msgFragment == null){
                    msgFragment = MessageFragment()
                    ft.add(R.id.main_fg_group,msgFragment as MessageFragment)
                }else{
                    ft.show(msgFragment!!)
                }
            }
            R.id.frame_mine_btn->{
                frameMineBtn!!.setSelFlag(true)
                if(mineFragment == null){
                    mineFragment = UserFragment()
                    ft.add(R.id.main_fg_group,mineFragment as UserFragment)
                }else{
                    ft.show(mineFragment!!)
                }

            }
        }
        ft.commit()
    }

    //隐藏framgnet
    fun hideFragment(){
       val manager = supportFragmentManager
        var ft = manager.beginTransaction()
        if(homeFragment!=null){
            ft.hide(homeFragment!!)
        }
        if(findFragment!=null){
            ft.hide(findFragment!!)
        }
        if(addFragment!=null){
            ft.hide(addFragment!!)
        }
        if(msgFragment!=null){
            ft.hide(msgFragment!!)
        }
        if(mineFragment!=null){
            ft.hide(mineFragment!!)
        }
        ft.commit()
    }

    //清除所有选择
    fun clearBottomsel(){
        frameHomeBtn!!.setSelFlag(false)
        frameFindBtn!!.setSelFlag(false)
        frameAddBtn!!.setSelFlag(false)
        frameMsgBtn!!.setSelFlag(false)
        frameMineBtn!!.setSelFlag(false)
    }


}